package net.ftzcode.util;

import net.ftzcode.model.Account;
import net.ftzcode.model.AccountType;
import net.ftzcode.model.Product;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by WangZiHe on 2017/9/27
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */
public class CuteUtils {

    public static final String FS_ACCOUNT = "FSAccount";
    public static final String FS_PRDTCODE = "FSPrdtCode";

    public static final String MD_ACCOUNT = "MDAccount";
    public static final String MD_PRDTCODE = "MDPrdtCode";


    public static void parserConf(List<Account> accountList, List<Product> productList) {
        String path = System.getProperty("user.dir") + "\\appConf.properties";

        ResourceBundle resourceBundle = null;
        try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(path))) {
            resourceBundle = new PropertyResourceBundle(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // ResourceBundle resourceBundle = ResourceBundle.getBundle("appConf");
        Iterator<String> iterator = resourceBundle.keySet().iterator();
        while (iterator.hasNext()) {
            String it = iterator.next();
            if (it.equals(FS_ACCOUNT) || it.equals(MD_ACCOUNT)) {
                String values = resourceBundle.getString(it);
                try {
                    parserAccount(it, values, accountList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (it.equals(FS_PRDTCODE) || it.equals(MD_PRDTCODE)) {
                String values = resourceBundle.getString(it);
                try {
                    parserProduct(it, values, productList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void parserProduct(String type, String values, List<Product> productList) throws Exception {
        AccountType accountType = null;
        if (type.equals(FS_PRDTCODE)) {
            accountType = AccountType.FUSHAN;
        } else if (type.equals(MD_PRDTCODE)) {
            accountType = AccountType.MINGDONG;
        }
        List<String> products = Arrays.asList(values.split(","));
        for (String value : products) {
            Product product = new Product();
            product.setAccountType(accountType);
            String acc[] = getProduct(value);
            if (acc.length < 2) {
                throw new Exception("账号参数配置错误:" + value);
            } else {
                product.setUrl(String.format(Product.productUrl, acc[0]));
                product.setCount(Integer.valueOf(acc[1]));
            }
            productList.add(product);
        }
    }

    public static void parserAccount(String type, String values, List<Account> accountList) throws Exception {
        AccountType accountType = null;
        if (type.equals(FS_ACCOUNT)) {
            accountType = AccountType.FUSHAN;
        } else if (type.equals(MD_ACCOUNT)) {
            accountType = AccountType.MINGDONG;
        }
        List<String> accounts = Arrays.asList(values.split(","));
        for (String value : accounts) {
            Account account = new Account();
            account.setAccountType(accountType);
            String acc[] = value.split("=");
            if (acc.length < 2) {
                throw new Exception("账号参数配置错误:" + value);
            } else {
                account.setUsername(acc[0]);
                account.setPassword(acc[1]);
            }
            accountList.add(account);
        }
    }


    public static String[] getProduct(String productCode) throws Exception {
        String product[] = new String[2];
        String f1 = "^(.*)\\[([0-9]{1,8})]$";
        //  System.out.println(productCode.matches(f1));
        Pattern pt = Pattern.compile(f1);
        Matcher match = pt.matcher(productCode);
        int countAll = match.groupCount();
        if (countAll == 2) {
            if (match.find()) {
                String pc = match.group(1);
                String count = match.group(2);
                product[0] = pc;
                product[1] = count;
                //  System.out.println("商品编码:" + pc);
                //  System.out.println("结果数量:" + count);
            }
        } else {
            throw new Exception("商品参数 设置不正确:" + productCode);
        }
        return product;
    }


  /*  public static void main(String[] args) {
        List<Account> accountList = new ArrayList<>();
        List<Product> productList = new ArrayList<>();
        parserConf(accountList, productList);

        accountList.stream().forEach(it -> {
            System.out.println("账号信息===》" + it.toString());
        });

        productList.stream().forEach(it -> {
            System.out.println("商品信息===》" + it.toString());
        });
    }*/

}
